package mamma.dao;

import java.util.HashMap;
import java.util.List;

import mamma.common.MybatisConfig;
import mamma.vo.BoardComment;
import mamma.vo.BoardVo;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class BoardDao {
	private SqlSessionFactory factory;
	
	// boardList
		public List<BoardVo> boardList(String state){
			List<BoardVo> list=null;
			
			/* 대용량의 경우 느림*/
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("state", state);
			
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			
			list = session.selectList("board.listBoard",map);
			session.close();
			
			return list;
			
		}
		//insertoBoard
		public int insertBoard(BoardVo boardVo){
			int result = 0;
			
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			result = session.insert("board.insertBoard",boardVo);
			session.commit();
			session.close();
			
			return result;
		}
		//board 읽기
		public BoardVo getBoard(int boardNum){
			BoardVo board = null;
			
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			session.update("board.viewcount",boardNum);
			board = session.selectOne("board.getBoard",boardNum);
			session.commit();
			session.close();

			
			return board;
		}
		//board 삭제
		public int deleteBoard(int boardNum){
			
			int result = 0;
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			result = session.delete("board.deleteBoard",boardNum);
			session.commit();
			session.close();

			return result;
		}
		
		//board 수정
		
		public int updateBoard(BoardVo boardVo){
			int result = 0;
			
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			result = session.update("board.updateBoard",boardVo);
			session.commit();
			session.close();
			
			return result;
		}
		
		//commentList
		public List<BoardComment> commentList(int boardNum){
			List<BoardComment> list = null;
			
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			list = session.selectList("board.listReply",boardNum);
			session.commit();
			session.close();
			
			return list;
		}
		//commentDelete
		public int deleteComment(int commentNum, String email){
			HashMap<String, Object> map = new HashMap<String, Object>();
			int result = 0;
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			map.put("commentNum", commentNum);
			map.put("email", email);
			result = session.delete("board.deleteReply",map);
			
			session.commit();
			session.close();

			return result;
		}
		//commentWrite
		public int writeComment(BoardComment comment){
			int result = 0;
			System.out.println(comment);
			factory = MybatisConfig.getSqlSessionFactory();
			SqlSession session = factory.openSession();
			result = session.insert("board.insertReply",comment);
			System.out.println(result);
			session.commit();
			session.close();
			return result;
		}
}
